﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using MM.Entities;
using MM.Business;

namespace MM
{
    public partial class frmDuyetPhieuXK : Form
    {
        string _sConn = "";
        string _nguoitao = "";
        string _matau = "";
        List<CHI_TIET_PXH> lstCT = new List<CHI_TIET_PXH>();
        PHIEU_XUAT_HANG objPXH = new PHIEU_XUAT_HANG();
        public frmDuyetPhieuXK(string sConn)
        {
            InitializeComponent();
            _sConn = sConn;
            _nguoitao = frmDangNhap.objTaiKhoan._soCMND;
            _matau = frmDangNhap.objTau._tenTau2KiTu;

            this.HienthiPHIEU_XUAT_HANG_MAX(_matau);
            this.HienthiCHI_TIET_PXH(this.txtMaPhieuXuatHang.Text.Trim());
        }
        private void HienthiPHIEU_XUAT_HANG(string MaPhieuXuatHang)
        {
            clsPHIEU_XUAT_HANG pxhCtrl = new clsPHIEU_XUAT_HANG(_sConn);
            List<PHIEU_XUAT_HANG> lst = new List<PHIEU_XUAT_HANG>();
            lst = pxhCtrl.usp_SelectPHIEU_XUAT_HANG(MaPhieuXuatHang);
            if (lst.Count > 0)
            {
                objPXH = new PHIEU_XUAT_HANG();
                objPXH = lst[0];
                this.txtNgayThucHien.Text = lst[0].NgayXuat.Day.ToString() + "/" + lst[0].NgayXuat.Month.ToString() + "/" + lst[0].NgayXuat.Year.ToString();
                this.txtNguoiThucHien.Text = lst[0].NguoiXuat;

                this.txtMaPhieuXuatHang.Text = lst[0].MaPhieuXuatHang;

                if (lst[0].NgayExport.HasValue)
                {
                    
                    DateTime dE = (DateTime)lst[0].NgayExport;
                    this.txtNgayDuyet.Text = dE.Day.ToString() + "/" + dE.Month.ToString() + "/" + dE.Year.ToString();
                }
                else
                {
                    this.txtNgayDuyet.Text = "";                    
                }

                if (lst[0].SoLanExport > 0)
                {
                    this.cmdDuyet.Visible = false;
                    this.txtGhiChu.Text = lst[0].GhiChu;
                    this.cmdXuatLai.Visible = true;
                }
                else
                {
                    this.cmdDuyet.Visible = true;
                    this.cmdXuatLai.Visible = false;
                }

            }

        }

        private void HienthiPHIEU_XUAT_HANG_MAX(string matau)
        {
            clsPHIEU_XUAT_HANG pxhCtrl = new clsPHIEU_XUAT_HANG(_sConn);
            List<PHIEU_XUAT_HANG> lst = new List<PHIEU_XUAT_HANG>();
            lst = pxhCtrl.usp_SelectPHIEU_XUAT_HANG_MAX(matau);
            if (lst.Count > 0)
            {
                objPXH = new PHIEU_XUAT_HANG();
                objPXH = lst[0];
                this.txtNgayThucHien.Text = lst[0].NgayXuat.Day.ToString() + "/" + lst[0].NgayXuat.Month.ToString() + "/" + lst[0].NgayXuat.Year.ToString();
                this.txtNguoiThucHien.Text = lst[0].NguoiXuat;
                if (lst[0].NgayExport.HasValue)
                {
                    this.txtNgayDuyet.Text = ((DateTime)lst[0].NgayExport).Day.ToString() + "/" + ((DateTime)lst[0].NgayExport).Month.ToString() + "/" + ((DateTime)lst[0].NgayExport).Year.ToString();
                }
                else
                {
                    this.txtNgayDuyet.Text = "";
                }
                this.txtMaPhieuXuatHang.Text = lst[0].MaPhieuXuatHang;
                if (lst[0].SoLanExport > 0)
                {
                    this.cmdDuyet.Visible = false;
                    this.txtGhiChu.Text = lst[0].GhiChu;
                    this.cmdXuatLai.Visible = true;
                }
                else
                {
                    this.cmdDuyet.Visible = true;
                    this.cmdXuatLai.Visible = false;
                }
            }

        }

        private void HienthiCHI_TIET_PXH(string MaPhieuXuatHang)
        {
            clsCHI_TIET_PXH ctpxhCtrl = new clsCHI_TIET_PXH(_sConn);
            
            lstCT = ctpxhCtrl.usp_SelectCHI_TIET_PXH_ByMaPhieuXuatHang(this.txtMaPhieuXuatHang.Text.Trim());
            lstCT.RemoveAll(delegate(CHI_TIET_PXH x) { return x.TinhTrang == 1; });
            this.gridControl1.DataSource = lstCT;
        }

        private void txtMaPhieuXuatHang_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (this.txtMaPhieuXuatHang.Text.Trim().Length == 10)
                {
                    this.HienthiCHI_TIET_PXH(this.txtMaPhieuXuatHang.Text.Trim());
                    this.HienthiPHIEU_XUAT_HANG(this.txtMaPhieuXuatHang.Text.Trim());
                }
            }
        }

        private void cmdDuyet_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.txtGhiChu.Text.Trim() == "")
                {
                    MessageBox.Show("Vui lòng nhập diễn giải cho đợt XUẤT KHO");
                    return;
                }

                string filepath = "";
                clsTHAM_SO tsCtrl = new clsTHAM_SO(_sConn);
                THAM_SO tsObj = new THAM_SO();
                tsObj = tsCtrl.SelectTHAM_SO("vDuyetPXK");

                if (tsObj != null && tsObj.DaXoa == 0)
                {
                    filepath = tsObj.GiaTri;
                }

                if (filepath == "")
                {
                    FolderBrowserDialog dg = new FolderBrowserDialog();
                    dg.ShowDialog();
                    filepath = dg.SelectedPath;
                }
                if (filepath != "")                  
                {
                    clsPHIEU_XUAT_HANG pxhCtrl = new clsPHIEU_XUAT_HANG(_sConn);
                    PHIEU_XUAT_HANG obj = new PHIEU_XUAT_HANG();
                    obj.MaPhieuXuatHang = this.txtMaPhieuXuatHang.Text.Trim();
                    obj.NgayExport = DateTime.Today;
                    obj.NguoiExport = _nguoitao;
                    obj.NgayXuat = DateTime.Today;
                    obj.NguoiXuat = _nguoitao;
                    obj.POThamChieuSAP = lstCT[0].POThamChieuSAP;
                    obj.GhiChu = this.txtGhiChu.Text;

                    if (obj.MaPhieuXuatHang.Length == 10)
                    {
                        int kq = pxhCtrl.usp_UpdatePHIEU_XUAT_HANG_Duyet(obj);
                        if (kq == 1)
                        {
                            // xuat file Csv
                            string y = DateTime.Today.Year.ToString();
                            string m = DateTime.Today.Month.ToString();
                            string d = DateTime.Today.Day.ToString();
                            m = Convert.ToString("0" + m).Substring(m.Length - 1, 2);
                            d = Convert.ToString("0" + d).Substring(d.Length - 1, 2);
                            filepath = filepath + "\\" + obj.MaPhieuXuatHang + "_" + y + m + d + ".csv";
                            // Xuat file Excel GI
                            pxhCtrl.CsvExportPHIEU_XUAT_HANG(obj, lstCT, filepath);

                            // thong bao cho nguoi dung
                            //MessageBox.Show("DUYỆT THÀNH CÔNG!" + "\n" + "GỬI EMAIL ĐÍNH KÈM FILE TẠI: " + filepath + " VỀ ĐỊA CHỈ: vmm@vitranschart.com.vn");
                            this.lblThongBao.Text = "DUYỆT THÀNH CÔNG!" + "\r\n" + "GỬI EMAIL ĐÍNH KÈM FILE TẠI: " + filepath + " VỀ ĐỊA CHỈ: vmm@vitranschart.com.vn";

                            this.cmdDuyet.Visible = false;
                        }
                        else
                            MessageBox.Show("DUYỆT KHÔNG THÀNH CÔNG");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void cmdXuatLai_Click(object sender, EventArgs e)
        {
            string filepath = "";
            clsTHAM_SO tsCtrl = new clsTHAM_SO(_sConn);
            THAM_SO tsObj = new THAM_SO();
            tsObj = tsCtrl.SelectTHAM_SO("vDuyetPXK");

            if (tsObj != null && tsObj.DaXoa == 0)
            {
                filepath = tsObj.GiaTri;
            }

            if (filepath == "")
            {
                FolderBrowserDialog dg = new FolderBrowserDialog();
                dg.ShowDialog();
                filepath = dg.SelectedPath;
            }
            if (filepath != "")           
            {
                clsPHIEU_XUAT_HANG pxhCtrl = new clsPHIEU_XUAT_HANG(_sConn);


                if (objPXH.MaPhieuXuatHang.Length == 10)
                {
                    objPXH.SoLanExport += 1;
                    int kq = pxhCtrl.usp_UpdatePHIEU_XUAT_HANG(objPXH);
                    if (kq == 1)
                    {
                        // xuat file Csv
                        string y = DateTime.Today.Year.ToString();
                        string m = DateTime.Today.Month.ToString();
                        string d = DateTime.Today.Day.ToString();
                        m = Convert.ToString("0" + m).Substring(m.Length - 1, 2);
                        d = Convert.ToString("0" + d).Substring(d.Length - 1, 2);
                        filepath = filepath + "\\" + objPXH.MaPhieuXuatHang + "_" + y + m + d + ".csv";
                        // Xuat file Excel GI
                        objPXH.POThamChieuSAP = lstCT[0].POThamChieuSAP;
                        pxhCtrl.CsvExportPHIEU_XUAT_HANG(objPXH, lstCT, filepath);

                        // thong bao cho nguoi dung
                        //MessageBox.Show("XUẤT LẠI THÀNH CÔNG!" + "\n" + "GỬI EMAIL ĐÍNH KÈM FILE TẠI: " + filepath + " VỀ ĐỊA CHỈ: vmm@vitranschart.com.vn");
                        this.lblThongBao.Text = "XUẤT LẠI THÀNH CÔNG!" + "\r\n" + "GỬI EMAIL ĐÍNH KÈM FILE TẠI: " + filepath + " VỀ ĐỊA CHỈ: vmm@vitranschart.com.vn";

                        this.cmdDuyet.Visible = false;
                    }
                    else
                        MessageBox.Show("DUYỆT KHÔNG THÀNH CÔNG");
                }
            }
        }

       

        private void frmDuyetPhieuXK_Load(object sender, EventArgs e)
        {
            Common.SetColor(this);
            this.lblThongBao.BackColor = this.BackColor;
        }

        private void label1_Click(object sender, EventArgs e)
        {
            frmTimPhieuXK f = new frmTimPhieuXK(_sConn);
            f.chonPXK += new frmTimPhieuXK.ChonPhieuXuatKho(GetPhieuXuatKho);
            f.Show(this);
        }

        private void GetPhieuXuatKho(object sender, ChonPhieuXKEventArgs e)
        {
            string mapxk = e.MaPhieuXuatKho;

            this.HienthiPHIEU_XUAT_HANG(mapxk);
            this.HienthiCHI_TIET_PXH(mapxk);
            
        }
    }
}